package com.bblocks.common.anti_reptile.rule;

import com.bblocks.common.anti_reptile.config.bean.RuleComm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 规则执行入口
 * @Author Zhao [125043150@qq.com]
 * @Date 2024/3/24 11:39
 * @Version 1.0
 * @since 8
 */
public class RuleActuator {

    private  List<AntiReptileRule> ruleList;

    public RuleActuator(List<AntiReptileRule> rules) {
        ruleList = rules;
    }

    /**
     * 是否允许通过请求，并
     * @param request 请求
     * @param response 响应
     * @return 请求是否允许通过(存在则不通过)
     */
    public RuleComm isAllowed(HttpServletRequest request , HttpServletResponse response){
        for (AntiReptileRule rule: ruleList){
            RuleComm ruleComm = rule.match(request,response);
            if (ruleComm != null){
                return ruleComm;
            }
        }
        return null;
    }

    /**
     * 重置验证码黑名单
     * @param request
     * @param realRequestUri
     */
    public void reset(HttpServletRequest request, String realRequestUri){
       ruleList.forEach(rule -> rule.reset(request, realRequestUri));
    }
}
